<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="../../layui/css/layui.css">
</head>

<body>

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>
            <button data-target="../User/select.html" class="layui-btn layui-btn-sm" id="reture">返回</button>
            修改用户
        </legend>
    </fieldset>

    <form class="layui-form" action="" onsubmit="return false" lay-filter="etidCou">

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">手机|账号</label>
                <div class="layui-input-inline">
                    <input type="tel" name="mobile" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="pwd" lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="user_edit">修改用户</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
    <script src="../../js/lodash.min.js"></script>
    <script src="../../js/mock-min.js"></script>
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.12.4.min.js"></script>
    <script src="../../js/index.js"></script>
    <script src="../../js/common.js"></script>

    <script>
        const href = query(location.href);
        const cid = href.id;
        layui.use('form', async function () {
            const form = layui.form;
            const cou = await $.getJSON("/api/User/" + cid);
            console.log(cou);
            form.val("etidCou", cou);
            //监听提交，修改课时事件
            form.on('submit(user_edit)', async function (data) {
                await $.ajax({
                    url: "/api/User/" + cid,
                    method: "put",
                    data: data.field,
                    success: function (data) {
                        console.log(data);
                        layer.msg('修改用户成功', {
                            icon: 1,
                            time: 1000
                        }, function () {
                            location.href = "./select.html";
                        })
                    }
                });
            });
            form.render(null);
        });

        function query(url) {
            const q = {};
            _.last(url.split("?")).split("&")
                .map(it => it.split("="))
                .forEach(([p, v]) => {
                    q[p] = decodeURIComponent(v);
                });
            return q;
        }

        let reture = document.getElementById("reture");
        reture.onclick = function () {
            location.href = "./select.html";
        }
    </script>

</body>

</html>